package cn.t.util.common;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

public class DateUtil {

    private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取当天字符串日期
     * @return xxx
     */
    public static String nowDateString() {
        return LocalDate.now().format(dateFormatter);
    }

    /**
     * 获取当天字符串时间
     * @return xxx
     */

    public static String nowDateTimeString() {
        return LocalDateTime.now().format(dateTimeFormatter);
    }

    /**
     * 解析日期
     * @param str xxx
     * @return xxx
     */

    public static LocalDate parseLocalDate(String str) {
        return LocalDate.parse(str, dateFormatter);
    }

    /**
     * 格式化日期
     * @param localDate xxx
     * @return xxx
     */

    public static String formatLocalDate(LocalDate localDate) {
        return localDate.format(dateFormatter);
    }

    /**
     * 解析时间
     * @param str xxx
     * @return xxx
     */

    public static LocalDateTime parseLocalDateTime(String str) {
        return LocalDateTime.parse(str, dateTimeFormatter);
    }

    /**
     * 格式化时间
     * @param localDateTime xxx
     * @return xxx
     */
    public static String formatLocalDateTime(LocalDateTime localDateTime) {
        return localDateTime.format(dateTimeFormatter);
    }

    /**
     * LocalDateTime转换为时间戳
     *
     * @param localDateTime 入参
     * @return 时间戳
     * */
    public static long localDateTimeToMills(LocalDateTime localDateTime) {
        return Timestamp.valueOf(localDateTime).getTime();
    }

    /**
     * 获取月首LocalDate
     * */
    public static LocalDate getBeginDateOfMonth() {
        return LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
    }

    /**
     * 获取月首LocalDateTime
     * */
    public static LocalDateTime getBeginDateTimeOfMonth() {
        return LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
    }

    public static LocalDate convertToLocalDateViaInstant(Date dateToConvert) {
        return dateToConvert.toInstant()
            .atZone(ZoneId.systemDefault())
            .toLocalDate();
    }
    public static LocalDate convertToLocalDateViaMillSecond(Date dateToConvert) {
        return Instant.ofEpochMilli(dateToConvert.getTime())
            .atZone(ZoneId.systemDefault())
            .toLocalDate();
    }

    public static LocalDate convertToLocalDateViaSqlDate(Date dateToConvert) {
        return new java.sql.Date(dateToConvert.getTime()).toLocalDate();
    }

    public static LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) {
        return dateToConvert.toInstant()
            .atZone(ZoneId.systemDefault())
            .toLocalDateTime();
    }

    public static LocalDateTime convertToLocalDateTimeViaMillSecond(Date dateToConvert) {
        return Instant.ofEpochMilli(dateToConvert.getTime())
            .atZone(ZoneId.systemDefault())
            .toLocalDateTime();
    }

    public static LocalDateTime convertToLocalDateTimeViaSqlTimestamp(Date dateToConvert) {
        return new java.sql.Timestamp(
            dateToConvert.getTime()).toLocalDateTime();
    }


    public static Date convertToDateViaSqlDate(LocalDate dateToConvert) {
        return java.sql.Date.valueOf(dateToConvert);
    }


    public static Date convertToDateViaInstant(LocalDate dateToConvert) {
        return java.util.Date.from(dateToConvert.atStartOfDay()
            .atZone(ZoneId.systemDefault())
            .toInstant());
    }


    public static Date convertToDateViaSqlTimestamp(LocalDateTime dateToConvert) {
        return java.sql.Timestamp.valueOf(dateToConvert);
    }

    public static Date convertToDateViaInstant(LocalDateTime dateToConvert) {
        return java.util.Date
            .from(dateToConvert.atZone(ZoneId.systemDefault())
                .toInstant());
    }



}
